package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Procurement;
import com.example.demo.entity.User;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.mapper.ProcurementMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/4/18
 */

@RestController
@RequestMapping("/bus-procurement")
public class BusProcurementController extends BaseController{
    @Resource
    ProcurementMapper procurementMapper;
    
    
    @Resource
    com.example.demo.mapper.OrderMapper OrderMapper;
    
    @PostMapping
    public Result<?> save(@RequestBody Procurement procurement) {
        procurementMapper.insert(procurement);
        return Result.success();
    }
    
    @PutMapping
    public Result<?> update(@RequestBody Procurement procurement) {
        User user = getUser();
        if(!procurement.getBusName().equals(user.getUsername())){
            return Result.error("1","账户错误");
        }
        Procurement procurement1 = procurementMapper.selectById(procurement.getId());
        if(procurement1.getBusName().equals(user.getUsername())){
            return Result.error("1","请勿重复报价");
        }
        procurementMapper.updateById(procurement);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        procurementMapper.deleteById(id);
        return Result.success();
    }
    
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        procurementMapper.deleteBatchIds(ids);
        return Result.success();
    }
    
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(procurementMapper.selectById(id));
    }
    
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Procurement> wrapper = Wrappers.<Procurement>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Procurement::getName, search);
        }
        wrapper.eq(Procurement::getStatus,"已发布");
        Page<Procurement> ProcurementPage = procurementMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(ProcurementPage);
    }
}
